#include "ModelMesh.h"
#include "BoundingSphereRenderer.h"

ModelMesh::ModelMesh()
{
}

ModelMesh::~ModelMesh()
{
}

void ModelMesh::Draw()
{
	glBindTexture(GL_TEXTURE_2D, m_pTexture->GetID());
	glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_pVertexBuffer->GetID());
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);

	int offset = 0;

	glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(offset));
	offset += sizeof(Vector3f);

	glColorPointer(4, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(offset));
	offset += sizeof(Color4);

	glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(offset));
	offset += sizeof(Vector3f);

	glTexCoordPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(offset));
	offset += sizeof(Vector2f);

	glDrawArrays(GL_TRIANGLES, 0, m_nVertexCount);

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);

	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
	glBindTexture(GL_TEXTURE_2D, 0);
}

void ModelMesh::UnloadContent()
{
	delete m_pIndexBuffer;
	delete m_pVertexBuffer;

	m_pIndexBuffer = NULL;
	m_pVertexBuffer = NULL;
}

IndexBuffer* ModelMesh::GetIndexBuffer() const
{
	return m_pIndexBuffer;
}

unsigned int ModelMesh::GetIndexCount() const
{
	return m_nIndexCount;
}

Texture2D* ModelMesh::GetTexture() const
{
	return m_pTexture;
}

VertexBuffer* ModelMesh::GetVertexBuffer() const
{
	return m_pVertexBuffer;
}

unsigned int ModelMesh::GetVertexCount() const
{
	return m_nVertexCount;
}